﻿using System;
using System.Collections.Generic;
using System.Text;
using AAA.Base;
using AAA.Base.Util;

namespace AAA.DataAccess.Meta
{
    public class CriteriaInfo
    {
//        private string _strOriginalParamName;
//        private string _strNewParamName;
//        private string _strNewValue;

        private char _cSplitDelimeter;

        public char SplitDelimeter
        {
            get { return _cSplitDelimeter; }
            set { _cSplitDelimeter = value; }
        }

        private string _strStatement;

        public string Statement
        {
            get { return _strStatement; }
            set { _strStatement = value; }
        }
        private string[] _strCheckFields;

        public string[] CheckFields
        {
            get { return _strCheckFields; }
            set { _strCheckFields = value; }
        }

        private NullValueProcessEnum _eNullValueProcess;

        public NullValueProcessEnum NullValueProcess
        {
            get { return _eNullValueProcess; }
            set { _eNullValueProcess = value; }
        }


        private CriteriaTypeEnum _eCriteriaType;

        public CriteriaTypeEnum CriteriaType
        {
            get { return _eCriteriaType; }
            set { _eCriteriaType = value; }
        }

        public CriteriaInfo(string strStatement, string[] strCheckFields, CriteriaTypeEnum eCriteriaType, NullValueProcessEnum eNullValueProcess)
        {
            Statement = strStatement;
            CheckFields = strCheckFields;
            CriteriaType = eCriteriaType;
            NullValueProcess = eNullValueProcess;
        }

/*
        public void ConvertParam(string[] strFields, object[] oArgs)
        {
            if (_strNewParamName == null)
                return;

            for (int i = 0; i < strFields.Length; i++)
                if (strFields[i] == _strOriginalParamName)
                {
                    strFields[i] = _strNewParamName;
                    oArgs[i] = _strNewValue;
                }
        }
*/
        public string GenerateStatement(Dictionary<string, object> dicParam, List<string> lstField)
        {
            bool hasNull = false;

            foreach (string strCheckField in CheckFields)
            {
                if (lstField.IndexOf(strCheckField) < 0)
                    lstField.Add(strCheckField);

                if (!dicParam.ContainsKey(strCheckField))
                {
                    hasNull = true;
                    break;
                }
            }
            if (hasNull && (NullValueProcess == NullValueProcessEnum.SKIP))
                return "";

            if(CriteriaType == CriteriaTypeEnum.Single)
                return Statement;

            for (int i = 0; i < CheckFields.Length; i++)
            {
                int iStartIndex = Statement.IndexOf("@" + CheckFields[i]) + 1;
                int iEndIndex = Statement.IndexOf(")", iStartIndex);
                string strParamName = Statement.Substring(iStartIndex, iEndIndex - iStartIndex).Trim();
                object[] oValues = ArrayHelper.ArrayFromObject(dicParam[strParamName]);

                if ((oValues.Length == 1) && (SplitDelimeter != null))
                {
                    oValues = oValues[0].ToString().Split(SplitDelimeter);
                }

                string strList = "";
                string strSingleParamName;
                lstField.Remove(strParamName);
                for (int j = 0; j < oValues.Length; j++)
                {
                    strSingleParamName = (strParamName + "_P" + j);
                    if(lstField.IndexOf(strSingleParamName) < 0)
                        lstField.Add(strSingleParamName);
                    strList += ", @" + strSingleParamName;
                    if(!dicParam.ContainsKey(strSingleParamName))
                        dicParam.Add(strSingleParamName, oValues[j]);
                }
                Statement = Statement.Replace("@" + strParamName, strList.StartsWith(",") ? strList.Substring(1) : strList);
            }
            return Statement;
/*
            string strBracket = CriteriaType == CriteriaTypeEnum.StringInList ? "'" : "";
            string strList = "";
            string strNewParamName = strParamName + "_LIST";

            for (int i = 0; i < oValues.Length; i++)
            {
                strList += "," + strBracket + oValues[i].ToString() + strBracket;
            }

            dicParam.Add(strNewParamName, strList.StartsWith(",") ? strList.Substring(1) : strList);

            _strOriginalParamName = strParamName;
            _strNewParamName = strNewParamName;
            _strNewValue = (string)dicParam[strNewParamName];
            return Statement.Replace("@" + strParamName, "@" + strNewParamName);
*/            
        }
    }
}
